package com.ccsoft.core;

import com.ccsoft.constant.Constant;

import java.io.File;

public class MergeInfoThread implements Runnable {
    private String fileName = "";
    private String[] fileNameArray = new String[Constant.THREAD_NUMBER];
    private long fileLength = 0;

    public MergeInfoThread(String fileName) {
        this.fileName = fileName;
        init();
    }

    @Override
    public void run() {
        long currentFileLength = 0;
        currentFileLength = new File(Constant.DEFAULT_LOCAL_PATH + fileName).length();

        String mergeInfo = "已合并大小：" + currentFileLength + "/" + fileLength + "，已合并百分比：" + currentFileLength / fileLength;
        System.out.print(mergeInfo);
    }

    private void init(){
        fileNameArray = getTmpFileNames();// 构建临时文件名数组

        // 计算多个临时文件大小之和
        for (int i = 0; i < fileNameArray.length; i++) {
            fileLength += new File(fileNameArray[i]).length();
        }
    }

    // 获取临时文件名构成的数组
    private String[] getTmpFileNames(){
        if( fileName == null || fileName.length() <=0 ) return null;

        String[] filenames = new String[Constant.THREAD_NUMBER];
        for (int i = 0; i < Constant.THREAD_NUMBER; i++) {
            filenames[i] = Constant.DEFAULT_LOCAL_PATH + fileName + Constant.TEMP_FILE_SUFFIX + i;
        }

        return filenames;
    }
}
